package cn.lingyangwl.agile.infra.gen.service.impl;

import cn.lingyangwl.agile.infra.gen.assembly.TemplateGroupAssembly;
import cn.lingyangwl.agile.infra.gen.manager.TemplateGroupManager;
import cn.lingyangwl.agile.infra.gen.mapper.TemplateConfigMapper;
import cn.lingyangwl.agile.infra.gen.model.entity.TemplateConfig;
import cn.lingyangwl.agile.infra.gen.model.entity.TemplateGroup;
import cn.lingyangwl.agile.infra.gen.model.rqrs.TemplateGroupListReq;
import cn.lingyangwl.agile.infra.gen.model.rqrs.TemplateGroupResp;
import cn.lingyangwl.agile.infra.gen.service.TemplateGroupService;
import cn.lingyangwl.framework.tool.core.StringUtils;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shenguangyang
 */
@Service
@RequiredArgsConstructor
public class TemplateGroupServiceImpl implements TemplateGroupService {
    private final TemplateConfigMapper templateConfigMapper;
    private final TemplateGroupManager templateGroupManager;
    private final TemplateGroupAssembly templateGroupAssembly;
    @Override
    public List<TemplateGroupResp> listTemplateGroup(TemplateGroupListReq req) {
        LambdaQueryWrapper<TemplateGroup> wrapper = new LambdaQueryWrapper<TemplateGroup>()
                .like(StringUtils.isNotEmpty(req.getGroupName()), TemplateGroup::getGroupName, req.getGroupName())
                .orderByDesc(TemplateGroup::getCreatedAt);
        return templateGroupManager.list(wrapper)
                .stream().map(templateGroupAssembly::toTemplateGroupRS).collect(Collectors.toList());
    }

    @Override
    public TemplateGroup getById(Long id) {
        return templateGroupManager.getById(id);
    }

    @Override
    public void save(TemplateGroup templateGroupPO) {
        TemplateGroup group = this.getByName(templateGroupPO.getGroupName());
        if (group != null) {
            throw new BizException(templateGroupPO.getGroupName() + " 已存在");
        }
        if (!templateGroupManager.save(templateGroupPO)) {
            throw new BizException("保存失败");
        }
    }

    @Override
    public void updateById(TemplateGroup templateGroupPO) {
        if (!templateGroupManager.updateById(templateGroupPO)) {
            throw new BizException("更新失败");
        }
    }

    @Override
    public void deleteById(TemplateGroup templateGroupPO) {
        if (!templateGroupManager.removeById(templateGroupPO)) {
            throw new BizException("删除失败");
        }
    }

    @Override
    @Transactional
    public void deleteGroup(TemplateGroup templateGroupPO) {
        List<TemplateGroup> templateGroupPOS = templateGroupManager.list();
        if (templateGroupPOS.size() == 1) {
            throw new BizException("无法删除，必须要有一个模板组");
        }
        if (!templateGroupManager.removeById(templateGroupPO.getId())) {
            throw new BizException("模板组删除失败");
        }

        LambdaQueryWrapper<TemplateConfig> wrapper = Wrappers.<TemplateConfig>lambdaQuery()
                .eq(TemplateConfig::getGroupId, templateGroupPO.getId());
        if (templateConfigMapper.selectCount(wrapper) > 0) {
            throw new BizException("模板组下还含有模板文件, 不能删除");
        }
    }

    @Override
    public TemplateGroup getByName(String name) {
        LambdaQueryWrapper<TemplateGroup> wrapper = Wrappers.<TemplateGroup>lambdaQuery()
                .like(TemplateGroup::getGroupName, name);
        return templateGroupManager.getOne(wrapper);
    }

    @Override
    public void deleteGroupBatch(List<Long> ids) {
        templateGroupManager.removeByIds(ids);
    }
}
